# from dotenv import load_dotenv
# # 导入环境.env文件
# load_dotenv()

from langchain_community.chat_models import ChatTongyi
from langchain_community.embeddings import DashScopeEmbeddings


def get_tongyi_llm():
    """
        加载tongyi大模型
    """
    return ChatTongyi(temperature=0.1, model="qwen-turbo")
def get_tongyi_qwq():
    """
        加载tongyi大模型
    """
    return ChatTongyi(temperature=0.1, model="qwq-32b")

def get_dashscope_embeddings():
    
    return DashScopeEmbeddings(model="text-embedding-v3")


#读取pdf文件
import PyPDF2
from langchain_text_splitters import RecursiveCharacterTextSplitter
import requests
import io
from utils.minio_util  import download_file_from_minio
# 1. 读取 PDF 文件内容
def read_pdf_embed(file_path):
    embed= get_dashscope_embeddings()
    # 发送请求获取远程 PDF 文件内容
    try:
        response = download_file_from_minio(file_path)
        # response.raise_for_status()  # 检查请求是否成功
        # 将响应内容转换为字节流
        pdf_stream = io.BytesIO(response.read())
        pdf_reader = PyPDF2.PdfReader(pdf_stream)
        #切分数据
        pdf_data = ""
                # 提取所有页面的文本
        for page in pdf_reader.pages:
            pdf_data += page.extract_text()
    except requests.exceptions.RequestException as e:
        print(f"请求远程 PDF 文件时发生错误: {e}")
        return None
    spliter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    spliters = spliter.split_text(pdf_data)
    embeddings = [embed.embed_query(text=text) for text in spliters]
    return spliters ,embeddings

from chromadb import HttpClient
import os
import uuid
def import_chromadb(rag_id,file_path):
    client = HttpClient(host=os.getenv("ChromaDB_HOST"), port=os.getenv("ChromaDB_PORT"))
    collection = client.get_or_create_collection(name=os.getenv("CollectionName"))
    spliters ,embeddings = read_pdf_embed(file_path)
    ids = [str(uuid.uuid4()) for i in range(len(spliters))]
    metadatas = [{"rag_id": rag_id} for _ in range(len(spliters))]
    collection.add(
        embeddings=embeddings,
        documents=spliters,
        ids=ids,
        metadatas=metadatas
    )
    return collection.get()

  
